#!/usr/bin/perl -w
# Author: Allen Hutchison
# Copyright (C) 2006 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Unit tests for Oaf::Config

use lib "../";
use Test::More tests => 17;

use strict;
use warnings;

BEGIN { use_ok('Oaf::Config') };

my $config = Oaf::Config->new('oaf.config_file' => 'test_config_file');
ok((defined($config) and (ref $config eq 'Oaf::Config')), 'new() test');

my $val = $config->get_param('dev32', 'item1');
is($val, 1, 'get_param for existing param');

$val = $config->get_param('foobar', 'foo');
is($val, undef, 'get_param for non-existing param with no default');

$val = $config->get_param('foobar', 'foobar', 42);
is($val, 42, 'get_param for non-existing param with default');

$val = $config->get_param('dev32', 'item1', 3);
is($val, 1, 'get_param for existing value with default');

$val = $config->set_param('foobar', 'bluebird', 56);
is($val, 56, 'set_param for a new section and value');

$val = $config->get_param('foobar', 'bluebird');
is($val, 56, 'get_param for a new section and value');

$config->set_param('foobar', 'bluebird', 100);
$val = $config->get_param('foobar', 'bluebird');
is($val, 100, 'set_param for an existing value');

$val = $config->_alias('dev1');
is($val, 'dev32', 'alias x 1');

$val = $config->_alias('dev4');
is($val, 'dev32', 'alias x 2');

$config->set_param(':aliases:', 'circle1', 'circle2');
$config->set_param(':aliases:', 'circle2', 'circle1');
$val = $config->_alias('circle1');
is($val, 'circle2', 'circular alias');

$config->set_param(':aliases:', 'circle1', 'circle2');
$config->set_param(':aliases:', 'circle2', 'circle3');
$config->set_param(':aliases:', 'circle3', 'circle4');
$config->set_param(':aliases:', 'circle4', 'circle1');
$val = $config->_alias('circle1');
is($val, 'circle4', 'long circular alias');

$config->set_param(':aliases:', 'circle5', 'circle5');
$val = $config->_alias('circle5');
is($val, 'circle5', 'self-referential alias');

$val = $config->get_param('dev1', 'item2');
is($val, 2, 'get_param with alias');

$config->set_param('captest', 'test', 1);
$config->set_param('CAPTEST', 'TEST', 2);
$val = $config->get_param('captest', 'test');
is($val, 2, 'mixed case set test');

$config->set_param('captest', 'test', 4);
$val = $config->get_param('CAPTEST', 'TEST');
is($val, 4, 'mixed case get test');
